/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shaman.storage.impl.stream.size;

import shaman.storage.impl.stream.gather.ScatterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import shaman.storage.impl.stream.filter.nullfilter.NullFilterOutputStream;

/**
 *
 * @author dmitriy
 */
public class SizeLimitOutputStream extends NullFilterOutputStream{

    private final long sizeLimit;
    private long offset=0;

    public SizeLimitOutputStream(OutputStream baseStream, long sizeLimit) {
        super(baseStream);
        this.sizeLimit=sizeLimit;
    }

    @Override
    public void write(int b) throws IOException {
        if (offset>=sizeLimit)
            throw new ScatterOutputStream.DeviceFullException();
        super.write(b);
        offset++;
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (offset+len>sizeLimit)
            throw new ScatterOutputStream.DeviceFullException();
        super.write(b, off, len);
        offset+=len;
    }

    public long getOffset() {
        return offset;
    }

}
